interface Position {
    x: number;
    y: number;
}

interface GameState {
    boardSize: number;
    wolfPositions: Position[];
    sheepPositions: Position[];
}
export class WolfScore{
  convertGameState(board):GameState{
    let wolfPositions=[]
    let sheepPositions=[]
    let x=0
    for (let row of board){
      let y=0
      for (let col of row){
        if (col==2){
          wolfPositions.push({ x,y })
        }else if(col==1){
          sheepPositions.push({x,y})
        }
        y++
      }
      x++
    }
    return {
      boardSize:5,
      wolfPositions,
      sheepPositions
    }
  }
  evaluateWolvesScore(gameState: GameState): number {
    const { boardSize, wolfPositions, sheepPositions } = gameState;

    // 1. 计算每个狼的活动范围
    let wolfActivityScore = 0;
    for (const wolfPosition of wolfPositions) {
      const wolfMoves = this.getWolfMoves(boardSize, wolfPosition, sheepPositions);
      wolfActivityScore += wolfMoves.length * 10; // 活动范围越大，分数越高
    }

    // 2. 剩余羊的数量
    const remainingSheep = sheepPositions.length;
    const sheepCountScore = -remainingSheep * 5; // 剩余羊越少，分数越高

    // 3. 狼被包围的程度
    let surroundedScore = 0;
    for (const wolfPosition of wolfPositions) {
      surroundedScore += this.calculateSurroundedScore(boardSize, wolfPosition, sheepPositions);
    }

    // 4. 潜在的吃羊机会
    let eatingOpportunitiesScore = 0;
    for (const wolfPosition of wolfPositions) {
      eatingOpportunitiesScore += this.getEatingOpportunities(boardSize, wolfPosition, sheepPositions).length * 20;
    }

    return wolfActivityScore + sheepCountScore + surroundedScore + eatingOpportunitiesScore;
  }

  private getWolfMoves(boardSize: number, wolfPosition: Position, sheepPositions: Position[]): Position[] {
    const moves: Position[] = [];
    const directions = [
      { x: 1, y: 0 },
      { x: -1, y: 0 },
      { x: 0, y: 1 },
      { x: 0, y: -1 }
    ];

    for (const dir of directions) {
      const newX = wolfPosition.x + dir.x;
      const newY = wolfPosition.y + dir.y;
      if (newX >= 0 && newX < boardSize && newY >= 0 && newY < boardSize && !sheepPositions.some(sheep => sheep.x === newX && sheep.y === newY)) {
        moves.push({ x: newX, y: newY });
      }
    }

    return moves;
  }

  private calculateSurroundedScore(boardSize: number, wolfPosition: Position, sheepPositions: Position[]): number {
    let score = 0;
    const directions = [
      { x: 1, y: 0 },
      { x: -1, y: 0 },
      { x: 0, y: 1 },
      { x: 0, y: -1 }
    ];

    for (const dir of directions) {
      const newX = wolfPosition.x + dir.x;
      const newY = wolfPosition.y + dir.y;
      if (newX >= 0 && newX < boardSize && newY >= 0 && newY < boardSize && sheepPositions.some(sheep => sheep.x === newX && sheep.y === newY)) {
        score -= 10; // 被羊挡住，分数减少
      }
    }

    return score;
  }

  private getEatingOpportunities(boardSize: number, wolfPosition: Position, sheepPositions: Position[]): Position[] {
    const opportunities: Position[] = [];
    const directions = [
      { x: 2, y: 0 },
      { x: -2, y: 0 },
      { x: 0, y: 2 },
      { x: 0, y: -2 }
    ];

    for (const dir of directions) {
      const newX = wolfPosition.x + dir.x;
      const newY = wolfPosition.y + dir.y;
      if (newX >= 0 && newX < boardSize && newY >= 0 && newY < boardSize) {
        const midX = wolfPosition.x + Math.floor(dir.x / 2);
        const midY = wolfPosition.y + Math.floor(dir.y / 2);
        if (sheepPositions.some(sheep => sheep.x === midX && sheep.y === midY) && !sheepPositions.some(sheep => sheep.x === newX && sheep.y === newY)) {
          opportunities.push({ x: newX, y: newY });
        }
      }
    }

    return opportunities;
  }
}


// 定义游戏状态
const gameState: GameState = {
    boardSize: 8,
    wolfPositions: [
        { x: 2, y: 2 }, // 第一只狼的位置
        { x: 3, y: 3 }, // 第二只狼的位置
        { x: 4, y: 4 }  // 第三只狼的位置
    ],
    sheepPositions: [
        { x: 1, y: 1 }, // 第一只羊的位置
        { x: 1, y: 2 }, // 第二只羊的位置
        { x: 2, y: 1 }, // 第三只羊的位置
        { x: 5, y: 5 }, // 第四只羊的位置
        { x: 6, y: 6 }, // 第五只羊的位置
        { x: 7, y: 7 }  // 第六只羊的位置
    ]
};

